package classification;

import java.util.Date;
import java.util.Hashtable;

import main.Paycheck;

import schedule.Period;

import timeCardTransaction.TimeCard;

public class HourlyClassification implements PaymentClassification {
	private double hourlyRate;
	private Hashtable<Date, TimeCard> timeCards = new Hashtable<Date, TimeCard>();

	public HourlyClassification(double rate) {
		this.hourlyRate = rate;
	}

	public double getHourlyRate() {
		return hourlyRate;
	}

	public TimeCard getTimeCard(Date date) {
		return timeCards.get(date);
	}

	public void addTimeCard(TimeCard card) {
		timeCards.put(card.getDate(), card);
	}

	public double calculatePay(Paycheck paycheck) {
		double totalPay = 0.0;

		for (TimeCard card : timeCards.values()) {
			if (Period.isInPayPeriod(card.getDate(),
					paycheck.getPayPeriodStartDate(),
					paycheck.getPayPeriodEndDate())) {
				totalPay += calculatePayForTimeCard(card);
			}
		}

		return totalPay;
	}

	private double calculatePayForTimeCard(TimeCard card) {
		double overtimeHours = Math.max(0.0, card.getHours() - 8);
		double normalHours = card.getHours() - overtimeHours;
		return hourlyRate * normalHours + hourlyRate * 1.5 * overtimeHours;
	}

	public String toString() {
		return "$" + hourlyRate + "/hr";
	}
}
